package com.example.chesstest;

import android.content.Context;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnDragListener;
import android.widget.FrameLayout;

public class PieceOnDragListener implements OnDragListener {
    private Context context;
    private boolean insideOfMe = false;
    private SquareAdapter squareAdapter;
  
    public PieceOnDragListener(Context pContext, SquareAdapter squareAdapter) {
      this.setContext(pContext);
      this.squareAdapter = squareAdapter;
    }
  
    @Override
	public boolean onDrag(View pV, DragEvent pEvent) {
		// TODO Auto-generated method stub
		final int lEventAction = pEvent.getAction();
	      switch(lEventAction) {
	      
	      case DragEvent.ACTION_DRAG_STARTED:
	        System.out.println("ACTION_DRAG_STARTED");
	        break;
	      case DragEvent.ACTION_DRAG_ENTERED:
	        insideOfMe = true;
	        System.out.println("ACTION_DRAG_ENTERED");
	        break;
	      case DragEvent.ACTION_DRAG_EXITED:
	        insideOfMe = false;
	        System.out.println("ACTION_DRAG_EXITED");
	        break;
	      case DragEvent.ACTION_DROP:
	        if (insideOfMe){
	          final View view = (View) pEvent.getLocalState();
	          final ViewGroup owner = (ViewGroup) view.getParent();
	          owner.removeView(view);
	          final FrameLayout container = (FrameLayout) pV;
	          container.addView(view);
	          
	          if(squareAdapter.getTurn())
	          {
	        	  squareAdapter.setTurn(false);
	          }
	          else if(!squareAdapter.getTurn())
	          {
	        	  squareAdapter.setTurn(true);
	          }
	          
	          System.out.println(view.getX());
	        }
	        System.out.println("ACTION_DROP");
	        break;
	      case DragEvent.ACTION_DRAG_ENDED:
	        System.out.println("ACTION_DRAG_ENDED");
	        break;
	      }
	      return true;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}
  }
